package ClientPackage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.KeyPair;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;

public class KeyGenerator {
	   
	public KeyGenerator(){
		
	}
	   
	public void generateKeys() throws IOException, NoSuchProviderException {
		String algorithm = "RSA";
		try {
			KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm);
		  	SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
			keyGen.initialize(1024, random);
		    KeyPair keyPair = keyGen.generateKeyPair();
		                        
		    //System.out.println(keyPair.getPublic());
		    //System.out.println(keyPair.getPrivate());
		    
		    PrivateKey privKey = keyPair.getPrivate();
			PublicKey  pubKey  = keyPair.getPublic();
			byte[] pubKeyBytes = pubKey.getEncoded();
			byte[] privKeyBytes = privKey.getEncoded();
			  
		    FileOutputStream privkeyfos = new FileOutputStream("client_private_key");
		    privkeyfos.write(privKeyBytes);
		    privkeyfos.close();
		    
		    FileOutputStream pubkeyfos = new FileOutputStream("client_public_key");
		    pubkeyfos.write(pubKeyBytes);
		    pubkeyfos.close();
		   
		  } catch (NoSuchAlgorithmException e) {
		    System.err.println( "usage: java AsymmetricKeyMaker <RSA | DSA>");
		  } catch (FileNotFoundException e) {
		  	 System.err.println( "File operation error");
		  }  
	   }
}
